package com.huatai.exchange.impl;

import com.alibaba.fastjson.JSON;
import com.faneui.common.bean.AbstractLogger;
import com.faneui.common.exception.BusinessException;
import com.faneui.common.utils.ValidateUtils;
import com.huatai.exchange.dal.dao.ExcCustDataFileDAO;
import com.huatai.exchange.dal.model.ExcCustDataFile;
import com.huatai.exchange.facade.CommunicationConstants;
import com.huatai.exchange.service.ExcCustDataFileService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ExcCustDataFileServiceImpl extends AbstractLogger implements ExcCustDataFileService {

    @Value("${server.port}")
    private String businSystemCode;

    @Autowired
    private ExcCustDataFileDAO excCustDataFileDAO;

    @Override
    public List<ExcCustDataFile> queryExcCustDataFileList(ExcCustDataFile excCustDataFile) throws BusinessException {
        List<ExcCustDataFile> list = null;
        try {
            list = excCustDataFileDAO.queryByModel(excCustDataFile);
        } catch (Exception ex) {
            BusinessException.throwBusinessException(ex);
        }
        return list;
    }

    @Override
    public ExcCustDataFile queryExcCustDataFile(String id) throws BusinessException {
        ExcCustDataFile dataFile = null;
        try {
            dataFile = excCustDataFileDAO.queryById(id);
        } catch (Exception ex) {
            BusinessException.throwBusinessException(ex);
        }
        return dataFile;
    }

    @Override
    public void modifyCustDataFile(ExcCustDataFile excCustDataFile) throws BusinessException {
        try {
            if (StringUtils.isNotBlank(excCustDataFile.getDestAddress())) {
                if ("1".equals(excCustDataFile.getDestType()) && !ValidateUtils.checkEmailsIsCorrect(excCustDataFile.getDestAddress())) {
                    throw new BusinessException("邮箱地址数据格式错误");
                }
            }
            String jsonStr = excCustDataFile.getJsonProperties();
            if(StringUtils.isNotBlank(jsonStr)) {
                Map<String,Object> map = JSON.parseObject(jsonStr) ;
                String toCcMails = (String) map.get(CommunicationConstants.MAIL_toCcMails) ;
                String toSeCcMails = (String)  map.get(CommunicationConstants.MAIL_toSeCcMails) ;
                if (StringUtils.isNotBlank(toCcMails) && !ValidateUtils.checkEmailsIsCorrect(toCcMails)) {
                    throw new BusinessException("抄送地址格式不正确");
                }
                if (StringUtils.isNotBlank(toSeCcMails) && !ValidateUtils.checkEmailsIsCorrect(toSeCcMails)) {
                    throw new BusinessException("密送地址格式不正确");
                }
            }
            Long num = excCustDataFileDAO.updateById(excCustDataFile);
            if (num == 0) {
                throw new BusinessException("更新失败");
            }
        } catch (Exception ex) {
            BusinessException.throwBusinessException(ex);
        }
    }

    @Override
    public void modifyCustDataFile(String id, String newNextDate, String oldNextDate) throws BusinessException {
        try {
            Long num = excCustDataFileDAO.updateNextDate(id, newNextDate, oldNextDate);
            if (num == 0) {
                throw new BusinessException("更新失败");
            }
        } catch (Exception ex) {
            BusinessException.throwBusinessException(ex);
        }
    }

}
